<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>LCOV - ged.info - gil/extension/io/jpeg_io_private.hpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LTP GCOV extension - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
        <tr>
          <td class="headerItem" width="20%">Current&nbsp;view:</td>
          <td class="headerValue" width="80%" colspan=4><a href="../../../index.html">directory</a> - <a href="index.html">gil/extension/io</a> - jpeg_io_private.hpp</td>
        </tr>
        <tr>
          <td class="headerItem" width="20%">Test:</td>
          <td class="headerValue" width="80%" colspan=4>ged.info</td>
        </tr>
        <tr>
          <td class="headerItem" width="20%">Date:</td>
          <td class="headerValue" width="20%">2009-06-09</td>
          <td width="20%"></td>
          <td class="headerItem" width="20%">Instrumented&nbsp;lines:</td>
          <td class="headerValue" width="20%">26</td>
        </tr>
        <tr>
          <td class="headerItem" width="20%">Code&nbsp;covered:</td>
          <td class="headerValue" width="20%">100.0 %</td>
          <td width="20%"></td>
          <td class="headerItem" width="20%">Executed&nbsp;lines:</td>
          <td class="headerValue" width="20%">26</td>
        </tr>
        </table>
      </td>
    </tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td><pre class="source">
<span class="lineNum">       1 </span>                : /*
<span class="lineNum">       2 </span>                :     Copyright 2005-2007 Adobe Systems Incorporated
<span class="lineNum">       3 </span>                :    
<span class="lineNum">       4 </span>                :     Use, modification and distribution are subject to the Boost Software License,
<span class="lineNum">       5 </span>                :     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
<span class="lineNum">       6 </span>                :     http://www.boost.org/LICENSE_1_0.txt).
<span class="lineNum">       7 </span>                : 
<span class="lineNum">       8 </span>                :     See http://opensource.adobe.com/gil for most recent version including documentation.
<span class="lineNum">       9 </span>                : */
<span class="lineNum">      10 </span>                : 
<span class="lineNum">      11 </span>                : /*************************************************************************************************/
<span class="lineNum">      12 </span>                : 
<span class="lineNum">      13 </span>                : #ifndef GIL_JPEG_IO_PRIVATE_H
<span class="lineNum">      14 </span>                : #define GIL_JPEG_IO_PRIVATE_H
<span class="lineNum">      15 </span>                : 
<span class="lineNum">      16 </span>                : /// \file
<span class="lineNum">      17 </span>                : /// \brief  Internal support for reading and writing JPEG files
<span class="lineNum">      18 </span>                : /// \author Hailin Jin and Lubomir Bourdev \n
<span class="lineNum">      19 </span>                : ///         Adobe Systems Incorporated
<span class="lineNum">      20 </span>                : /// \date   2005-2007 \n Last updated September 24, 2006
<span class="lineNum">      21 </span>                : 
<span class="lineNum">      22 </span>                : #include &lt;stdio.h&gt;
<span class="lineNum">      23 </span>                : #include &lt;boost/static_assert.hpp&gt;
<span class="lineNum">      24 </span>                : #include &lt;vector&gt;
<span class="lineNum">      25 </span>                : #include &quot;../../gil_all.hpp&quot;
<span class="lineNum">      26 </span>                : #include &quot;io_error.hpp&quot;
<span class="lineNum">      27 </span>                : 
<span class="lineNum">      28 </span>                : namespace boost { namespace gil {
<span class="lineNum">      29 </span>                : 
<span class="lineNum">      30 </span>                : namespace detail {
<span class="lineNum">      31 </span>                : 
<span class="lineNum">      32 </span>                : // lbourdev: What is the advantage of having channel and colorspace together? Are there cases where they are interrelated?
<span class="lineNum">      33 </span>                : 
<span class="lineNum">      34 </span>                : template &lt;typename Channel,typename ColorSpace&gt;
<span class="lineNum">      35 </span>                : struct jpeg_read_support_private {
<span class="lineNum">      36 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=false);
<span class="lineNum">      37 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_UNKNOWN);
<span class="lineNum">      38 </span>                : };
<span class="lineNum">      39 </span>                : template &lt;&gt;
<span class="lineNum">      40 </span>                : struct jpeg_read_support_private&lt;bits8,gray_t&gt; {
<span class="lineNum">      41 </span>                :     BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
<span class="lineNum">      42 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=true);
<span class="lineNum">      43 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_GRAYSCALE);
<span class="lineNum">      44 </span>                : };
<span class="lineNum">      45 </span>                : template &lt;&gt;
<span class="lineNum">      46 </span>                : struct jpeg_read_support_private&lt;bits8,rgb_t&gt; {
<span class="lineNum">      47 </span>                :     BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
<span class="lineNum">      48 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=true);
<span class="lineNum">      49 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_RGB);
<span class="lineNum">      50 </span>                : };
<span class="lineNum">      51 </span>                : template &lt;&gt;
<span class="lineNum">      52 </span>                : struct jpeg_read_support_private&lt;bits8,cmyk_t&gt; {
<span class="lineNum">      53 </span>                :     BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
<span class="lineNum">      54 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=true);
<span class="lineNum">      55 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_CMYK);
<span class="lineNum">      56 </span>                : };
<span class="lineNum">      57 </span>                : template &lt;typename Channel,typename ColorSpace&gt;
<span class="lineNum">      58 </span>                : struct jpeg_write_support_private {
<span class="lineNum">      59 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=false);
<span class="lineNum">      60 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_UNKNOWN);
<span class="lineNum">      61 </span>                : };
<span class="lineNum">      62 </span>                : template &lt;&gt;
<span class="lineNum">      63 </span>                : struct jpeg_write_support_private&lt;bits8,gray_t&gt; {
<span class="lineNum">      64 </span>                :     BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
<span class="lineNum">      65 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=true);
<span class="lineNum">      66 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_GRAYSCALE);
<span class="lineNum">      67 </span>                : };
<span class="lineNum">      68 </span>                : template &lt;&gt;
<span class="lineNum">      69 </span>                : struct jpeg_write_support_private&lt;bits8,rgb_t&gt; {
<span class="lineNum">      70 </span>                :     BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
<span class="lineNum">      71 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=true);
<span class="lineNum">      72 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_RGB);
<span class="lineNum">      73 </span>                : };
<span class="lineNum">      74 </span>                : template &lt;&gt;
<span class="lineNum">      75 </span>                : struct jpeg_write_support_private&lt;bits8,cmyk_t&gt; {
<span class="lineNum">      76 </span>                :     BOOST_STATIC_ASSERT(BITS_IN_JSAMPLE==8);
<span class="lineNum">      77 </span>                :     BOOST_STATIC_CONSTANT(bool,is_supported=true);
<span class="lineNum">      78 </span>                :     BOOST_STATIC_CONSTANT(J_COLOR_SPACE,color_type=JCS_CMYK);
<span class="lineNum">      79 </span>                : };
<span class="lineNum">      80 </span>                : 
<span class="lineNum">      81 </span>                : 
<span class="lineNum">      82 </span>                : class jpeg_reader : public file_mgr {
<span class="lineNum">      83 </span>                : protected:
<span class="lineNum">      84 </span>                :     jpeg_decompress_struct  _cinfo;
<span class="lineNum">      85 </span>                :     jpeg_error_mgr          _jerr;
<span class="lineNum">      86 </span>                : 
<span class="lineNum">      87 </span><span class="lineCov">              6 :     void init() {</span>
<span class="lineNum">      88 </span><span class="lineCov">              6 :         _cinfo.err=jpeg_std_error(&amp;_jerr);</span>
<span class="lineNum">      89 </span><span class="lineCov">              6 :         jpeg_create_decompress(&amp;_cinfo);</span>
<span class="lineNum">      90 </span><span class="lineCov">              6 :         jpeg_stdio_src(&amp;_cinfo,_fp.get());</span>
<span class="lineNum">      91 </span><span class="lineCov">              6 :         jpeg_read_header(&amp;_cinfo,TRUE);</span>
<span class="lineNum">      92 </span><span class="lineCov">              6 :     }</span>
<span class="lineNum">      93 </span>                : public:
<span class="lineNum">      94 </span>                :     jpeg_reader(FILE* file)           : file_mgr(file)           { init(); }
<span class="lineNum">      95 </span><span class="lineCov">              6 :     jpeg_reader(const char* filename) : file_mgr(filename, &quot;rb&quot;) { init(); }</span>
<span class="lineNum">      96 </span>                : 
<span class="lineNum">      97 </span><span class="lineCov">              6 :     ~jpeg_reader() { jpeg_destroy_decompress(&amp;_cinfo); }</span>
<span class="lineNum">      98 </span>                : 
<span class="lineNum">      99 </span>                :     template &lt;typename View&gt;
<span class="lineNum">     100 </span><span class="lineCov">              6 :     void apply(const View&amp; view) {</span>
<span class="lineNum">     101 </span><span class="lineCov">              6 :         jpeg_start_decompress(&amp;_cinfo);    // lbourdev: Can this return an error? You need to check and throw. Check all other library methods that can return an error state...</span>
<span class="lineNum">     102 </span><span class="lineCov">              6 :         io_error_if(_cinfo.data_precision!=8,&quot;jpeg_reader::apply(): this image file is not supported&quot;);</span>
<span class="lineNum">     103 </span><span class="lineCov">              6 :         io_error_if(_cinfo.out_color_space!=jpeg_read_support_private&lt;typename channel_type&lt;View&gt;::type,</span>
<span class="lineNum">     104 </span>                :                                                                       typename color_space_type&lt;View&gt;::type&gt;::color_type,
<span class="lineNum">     105 </span>                :                     &quot;jpeg_reader::apply(): input view type does not match the image file&quot;);
<span class="lineNum">     106 </span><span class="lineCov">              6 :         io_error_if(view.dimensions() != get_dimensions(), &quot;jpeg_reader::apply(): input view dimensions do not match the image file&quot;);</span>
<span class="lineNum">     107 </span><span class="lineCov">             12 :         std::vector&lt;pixel&lt;bits8,layout&lt;typename color_space_type&lt;View&gt;::type&gt; &gt; &gt; row(view.width());</span>
<span class="lineNum">     108 </span><span class="lineCov">             12 :         JSAMPLE* row_address=(JSAMPLE*)&amp;row.front();</span>
<span class="lineNum">     109 </span><span class="lineCov">            522 :         for(int y=0;y&lt;view.height();++y) {</span>
<span class="lineNum">     110 </span><span class="lineCov">            516 :             io_error_if(jpeg_read_scanlines(&amp;_cinfo,(JSAMPARRAY)&amp;row_address,1)!=1,</span>
<span class="lineNum">     111 </span>                :                         &quot;jpeg_reader::apply(): fail to read JPEG file&quot;);
<span class="lineNum">     112 </span><span class="lineCov">            516 :             std::copy(row.begin(),row.end(),view.row_begin(y));</span>
<span class="lineNum">     113 </span>                :         }
<span class="lineNum">     114 </span><span class="lineCov">              6 :         jpeg_finish_decompress(&amp;_cinfo);</span>
<span class="lineNum">     115 </span><span class="lineCov">              6 :     }</span>
<span class="lineNum">     116 </span>                :     
<span class="lineNum">     117 </span>                :     template &lt;typename Image&gt;
<span class="lineNum">     118 </span><span class="lineCov">              6 :     void read_image(Image&amp; im) {</span>
<span class="lineNum">     119 </span><span class="lineCov">              6 :         im.recreate(get_dimensions());</span>
<span class="lineNum">     120 </span><span class="lineCov">              6 :         apply(view(im));</span>
<span class="lineNum">     121 </span><span class="lineCov">              6 :     }</span>
<span class="lineNum">     122 </span>                : 
<span class="lineNum">     123 </span><span class="lineCov">             12 :     point2&lt;std::ptrdiff_t&gt; get_dimensions() const {</span>
<span class="lineNum">     124 </span><span class="lineCov">             12 :         return point2&lt;std::ptrdiff_t&gt;(_cinfo.image_width,_cinfo.image_height);</span>
<span class="lineNum">     125 </span>                :     }
<span class="lineNum">     126 </span>                : };
<span class="lineNum">     127 </span>                : 
<span class="lineNum">     128 </span>                : // This code will be simplified...
<span class="lineNum">     129 </span>                : template &lt;typename CC&gt;
<span class="lineNum">     130 </span>                : class jpeg_reader_color_convert : public jpeg_reader {
<span class="lineNum">     131 </span>                : private:
<span class="lineNum">     132 </span>                :     CC _cc;
<span class="lineNum">     133 </span>                : public:
<span class="lineNum">     134 </span>                :     jpeg_reader_color_convert(FILE* file,CC cc_in)           : jpeg_reader(file),_cc(cc_in) {}
<span class="lineNum">     135 </span>                :     jpeg_reader_color_convert(FILE* file)           : jpeg_reader(file) {}
<span class="lineNum">     136 </span>                :     jpeg_reader_color_convert(const char* filename,CC cc_in) : jpeg_reader(filename),_cc(cc_in) {}
<span class="lineNum">     137 </span>                :     jpeg_reader_color_convert(const char* filename) : jpeg_reader(filename) {}
<span class="lineNum">     138 </span>                :     template &lt;typename View&gt;
<span class="lineNum">     139 </span>                :     void apply(const View&amp; view) {
<span class="lineNum">     140 </span>                :         jpeg_start_decompress(&amp;_cinfo);    // lbourdev: Can this return an error? You need to check and throw. Check all other library methods that can return an error state...
<span class="lineNum">     141 </span>                :         io_error_if(_cinfo.data_precision!=8,&quot;jpeg_reader_color_covert::apply(): this image file is not supported&quot;);
<span class="lineNum">     142 </span>                :         io_error_if(view.dimensions() != get_dimensions(), &quot;jpeg_reader_color_covert::apply(): input view dimensions don't match the image file&quot;);
<span class="lineNum">     143 </span>                :         switch (_cinfo.out_color_space) {
<span class="lineNum">     144 </span>                :         case JCS_GRAYSCALE: {
<span class="lineNum">     145 </span>                :             std::vector&lt;gray8_pixel_t&gt; row(view.width());
<span class="lineNum">     146 </span>                :             JSAMPLE* row_address=(JSAMPLE*)&amp;row.front();
<span class="lineNum">     147 </span>                :             for(int y=0;y&lt;view.height();++y) {
<span class="lineNum">     148 </span>                :                 io_error_if(jpeg_read_scanlines(&amp;_cinfo,(JSAMPARRAY)&amp;row_address,1)!=1,
<span class="lineNum">     149 </span>                :                             &quot;jpeg_reader_color_covert::apply(): fail to read JPEG file&quot;);
<span class="lineNum">     150 </span>                :                 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;gray8_ref_t, typename View::value_type,CC&gt;(_cc));
<span class="lineNum">     151 </span>                :             }
<span class="lineNum">     152 </span>                :             break;
<span class="lineNum">     153 </span>                :         }
<span class="lineNum">     154 </span>                :         case JCS_RGB: {
<span class="lineNum">     155 </span>                :             std::vector&lt;rgb8_pixel_t&gt; row(view.width());
<span class="lineNum">     156 </span>                :             JSAMPLE* row_address=(JSAMPLE*)&amp;row.front();
<span class="lineNum">     157 </span>                :             for(int y=0;y&lt;view.height();++y) {
<span class="lineNum">     158 </span>                :                 io_error_if(jpeg_read_scanlines(&amp;_cinfo,(JSAMPARRAY)&amp;row_address,1)!=1,
<span class="lineNum">     159 </span>                :                             &quot;jpeg_reader_color_covert::apply(): fail to read JPEG file&quot;);
<span class="lineNum">     160 </span>                :                 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;rgb8_ref_t, typename View::value_type,CC&gt;(_cc));
<span class="lineNum">     161 </span>                :             }
<span class="lineNum">     162 </span>                :             break;
<span class="lineNum">     163 </span>                :         }
<span class="lineNum">     164 </span>                :         case JCS_CMYK: {
<span class="lineNum">     165 </span>                :             std::vector&lt;cmyk8_pixel_t&gt; row(view.width());
<span class="lineNum">     166 </span>                :             JSAMPLE* row_address=(JSAMPLE*)&amp;row.front();
<span class="lineNum">     167 </span>                :             for(int y=0;y&lt;view.height();++y) {
<span class="lineNum">     168 </span>                :                 io_error_if(jpeg_read_scanlines(&amp;_cinfo,(JSAMPARRAY)&amp;row_address,1)!=1,
<span class="lineNum">     169 </span>                :                             &quot;jpeg_reader_color_covert::apply(): fail to read JPEG file&quot;);
<span class="lineNum">     170 </span>                :                 std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;cmyk8_ref_t, typename View::value_type,CC&gt;(_cc));
<span class="lineNum">     171 </span>                :             }
<span class="lineNum">     172 </span>                :             break;
<span class="lineNum">     173 </span>                :         }
<span class="lineNum">     174 </span>                :         default:
<span class="lineNum">     175 </span>                :             io_error(&quot;jpeg_reader_color_covert::apply(): unknown color type&quot;);
<span class="lineNum">     176 </span>                :         }
<span class="lineNum">     177 </span>                :         jpeg_finish_decompress(&amp;_cinfo);
<span class="lineNum">     178 </span>                :     }    
<span class="lineNum">     179 </span>                :     template &lt;typename Image&gt;
<span class="lineNum">     180 </span>                :     void read_image(Image&amp; im) {
<span class="lineNum">     181 </span>                :         im.recreate(get_dimensions());
<span class="lineNum">     182 </span>                :         apply(view(im));
<span class="lineNum">     183 </span>                :     }
<span class="lineNum">     184 </span>                : };
<span class="lineNum">     185 </span>                : 
<span class="lineNum">     186 </span>                : class jpeg_writer : public file_mgr {
<span class="lineNum">     187 </span>                :     jpeg_compress_struct _cinfo;
<span class="lineNum">     188 </span>                :     jpeg_error_mgr _jerr;
<span class="lineNum">     189 </span>                : 
<span class="lineNum">     190 </span>                :     void init() {
<span class="lineNum">     191 </span>                :         _cinfo.err=jpeg_std_error(&amp;_jerr);
<span class="lineNum">     192 </span>                :         jpeg_create_compress(&amp;_cinfo);
<span class="lineNum">     193 </span>                :         jpeg_stdio_dest(&amp;_cinfo,_fp.get());
<span class="lineNum">     194 </span>                :     }
<span class="lineNum">     195 </span>                : public:
<span class="lineNum">     196 </span>                :     jpeg_writer(FILE* file)           : file_mgr(file)           { init(); }
<span class="lineNum">     197 </span>                :     jpeg_writer(const char* filename) : file_mgr(filename, &quot;wb&quot;) { init(); }
<span class="lineNum">     198 </span>                :     ~jpeg_writer() { jpeg_destroy_compress(&amp;_cinfo); }
<span class="lineNum">     199 </span>                :     
<span class="lineNum">     200 </span>                :     template &lt;typename View&gt;
<span class="lineNum">     201 </span>                :     void apply(const View&amp; view,int quality=100) {
<span class="lineNum">     202 </span>                :         _cinfo.image_width  = (JDIMENSION)view.width();
<span class="lineNum">     203 </span>                :         _cinfo.image_height = (JDIMENSION)view.height();
<span class="lineNum">     204 </span>                :         _cinfo.input_components=num_channels&lt;View&gt;::value;
<span class="lineNum">     205 </span>                :         _cinfo.in_color_space = jpeg_write_support_private&lt;typename channel_type&lt;View&gt;::type,
<span class="lineNum">     206 </span>                :                                                            typename color_space_type&lt;View&gt;::type&gt;::color_type;
<span class="lineNum">     207 </span>                :         jpeg_set_defaults(&amp;_cinfo);
<span class="lineNum">     208 </span>                :         jpeg_set_quality(&amp;_cinfo, quality, TRUE);
<span class="lineNum">     209 </span>                :         jpeg_start_compress(&amp;_cinfo, TRUE);
<span class="lineNum">     210 </span>                :         std::vector&lt;pixel&lt;bits8,layout&lt;typename color_space_type&lt;View&gt;::type&gt; &gt; &gt; row(view.width());
<span class="lineNum">     211 </span>                :         JSAMPLE* row_address=(JSAMPLE*)&amp;row.front();
<span class="lineNum">     212 </span>                :         for (int y=0;y&lt;view.height(); ++y) {
<span class="lineNum">     213 </span>                :             std::copy(view.row_begin(y),view.row_end(y),row.begin());
<span class="lineNum">     214 </span>                :             io_error_if(jpeg_write_scanlines(&amp;_cinfo,(JSAMPARRAY)&amp;row_address,1) != 1,
<span class="lineNum">     215 </span>                :                         &quot;jpeg_writer::apply(): fail to write file&quot;);
<span class="lineNum">     216 </span>                :         }
<span class="lineNum">     217 </span>                :         jpeg_finish_compress(&amp;_cinfo);
<span class="lineNum">     218 </span>                :     }
<span class="lineNum">     219 </span>                : };
<span class="lineNum">     220 </span>                : 
<span class="lineNum">     221 </span>                : } // namespace detail
<span class="lineNum">     222 </span>                : 
<span class="lineNum">     223 </span>                : } }  // namespace boost::gil
<span class="lineNum">     224 </span>                : 
<span class="lineNum">     225 </span>                : #endif
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
  <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LTP GCOV extension version 1.6</a></td></tr>
  </table>
  <br>

</body>
</html>
